package tt.dz.mvc.controller.sys;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.system.OmsUrlList;
import tt.dz.entity.system.OmsUrlTreeVO;
import tt.dz.mvc.controller.base.BaseAction;
import tt.dz.service.sys.UrlListService;
import tt.dz.util.ActionUtils;
import tt.dz.util.OmsConstant;

@Controller
@RequestMapping("urlAuthApi")
public class UrlAction extends BaseAction
{
	@Autowired
	private UrlListService urlListServiceImpl;
	
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> addUrl(HttpServletRequest request,HttpServletResponse response)
	{
		return urlListServiceImpl.add(this.genarateObj(request, this.getLoginUserId(request), true,false));
	}
	
	@RequestMapping(value="update/{url_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> updateUrl(@PathVariable String url_id,
			HttpServletRequest request,HttpServletResponse response)
	{
		OmsUrlList url = this.genarateObj(request, this.getLoginUserId(request), false,false);
		url.setUrl_id(url_id);
		return urlListServiceImpl.update(url);
	}
	
	@RequestMapping(value="delete/{url_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteUrl(@PathVariable String url_id,
			HttpServletRequest request,HttpServletResponse response)
	{
		return urlListServiceImpl.delete(url_id);
	}
	
	@RequestMapping(value="findPagOrAll",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findPagOrAll(HttpServletRequest request,HttpServletResponse response)
	{
		OmsUrlList url = this.genarateObj(request, this.getLoginUserId(request), false,true);
		return urlListServiceImpl.findPageOrAll(this.buildQueryParam(request, true), url);
	}
	
	@RequestMapping(value="findExistsUrlByRoleId",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findExistsUrlByRoleId(HttpServletRequest request,HttpServletResponse response)
	{
		String roleId = request.getParameter("roleId");
		String levelType = request.getParameter("levleType");
		String isExists = request.getParameter("isExists");
		return urlListServiceImpl.findUrlListByRoleId(roleId, levelType, "1".equals(isExists));
	}
	
	@RequestMapping(value="findAllUrlByRoleId",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findAllUrlByRoleId(HttpServletRequest request,HttpServletResponse response)
	{
		String roleId = request.getParameter("roleId");
		return urlListServiceImpl.findUrlListByRoleId(roleId);
	}
	
	@RequestMapping(value="findById/{url_id}",method=RequestMethod.GET)
	@ResponseBody
	public JsonCommon<?> findById(@PathVariable String url_id,HttpServletRequest request,HttpServletResponse response)
	{
		OmsUrlList url = this.genarateObj(request, this.getLoginUserId(request), false, true);
		url.setUrl_id(url_id);
		return urlListServiceImpl.findById(url);
	}
	
	private OmsUrlList genarateObj(HttpServletRequest request,String userId,boolean isCreate,boolean isFind)
	{
		Map<String,String> params = ActionUtils.getParameterMap(request);
		return ActionUtils.getParamInstance(OmsUrlList.class, params, userId, isCreate, isFind);
	}
}
